package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.beta.dto.user.ReceiverDto;
import com.beta.entity.Receivers;
import com.beta.mapper.ReceiversMapper;
import com.beta.service.IReceiversService;
import com.beta.service.ServiceException;
import com.beta.service.IDateTimeService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Receivers 表数据服务层接口实现类
 */
@Service
public class ReceiversService extends SuperServiceImpl<ReceiversMapper, Receivers> implements IReceiversService {

    @Autowired
    private IDateTimeService dbService;

    @Autowired
    private MapperFacade beanMapper;

    @Override
    public ReceiverDto addReceiver(Integer userId, ReceiverDto receiver) {

        verifyUserId(userId);

        Receivers newReceiver = convertToEntity(userId, receiver);

        Receivers dbReceiver = selectOne(newReceiver);
        if (Objects.isNull(dbReceiver)) {
            Date toDbDate = dbService.toDbDate();
            newReceiver.setCreatedAt(toDbDate);
            newReceiver.setUpdatedAt(toDbDate);
            insertSelective(newReceiver);

            newReceiver.setCreatedAt(null);
            newReceiver.setUpdatedAt(null);
            dbReceiver = selectOne(newReceiver);
        } else {
            // ignore
        }

        return beanMapper.map(dbReceiver, ReceiverDto.class);
    }

    private Receivers convertToEntity(Integer userId, ReceiverDto receiverDto) {
        Receivers receivers = new Receivers();
        receivers.setUserId(userId);
        receivers.setAddress(receiverDto.getAddress());
        receivers.setUserName(receiverDto.getUserName());
        receivers.setUserPhone(receiverDto.getUserPhone());
        receivers.setProvince(receiverDto.getProvince());
        receivers.setCity(receiverDto.getCity());
        receivers.setAddress(receiverDto.getAddress());
        receivers.setDistrict(receiverDto.getDistrict());
        receivers.setDistrictCode(receiverDto.getDistrictCode());
        receivers.setStreet(receiverDto.getStreet());
        return receivers;
    }

    private void verifyUserId(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new ServiceException("E00113");
        }
    }

    @Override
    public ReceiverDto updateReceiver(Integer receiverId, ReceiverDto receiver) {
        Receivers receivers = convertToEntity(null, receiver);
        receivers.setId(receiverId);
        receivers.setUpdatedAt(dbService.toDbDate());
        updateSelectiveById(receivers);
        return beanMapper.map(selectById(receiverId), ReceiverDto.class);
    }

    @Override
    public void deleteReceiver(Integer receiveId) {
        if (Objects.isNull(receiveId)) {
            throw new ServiceException("E10007");
        }
        deleteById(receiveId);
    }

    @Override
    public List<ReceiverDto> getReceiversByUserId(Integer userId) {

        verifyUserId(userId);

        EntityWrapper<Receivers> receiversEntityWrapper = new EntityWrapper<>(new Receivers().setUserId(userId));
        List<Receivers> receivers = selectList(receiversEntityWrapper.orderBy("created_at", false));
        return beanMapper.mapAsList(receivers, ReceiverDto.class);
    }

}